/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.swing.help;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Shows a Help JFrame with a JTabbedPane containig help as well as other information.
 * 
 * @author Francisco Morero Peyrona
 */

public class JHelp extends JTabbedPane
{
	// Elements defined (one application element per HTML page)
	public final static String TREE      = "tree";
	public final static String DESKTOP   = "desktop";
	public final static String VERBOSER  = "verboser";

	public final static String TABLE     = "table";
	public final static String PROCEDURE = "procedure";
	public final static String DIAGRAM   = "diagram";
	public final static String UDT       = "udt";

	private ContextHelp help;
    private SystemPanel system;
    private MemMon      memon;

	//-------------------------------------------------------------------------//

	/**
	 * Class constructor
	 */
	public JHelp()
	{
		this.help   = new ContextHelp();
        this.system = new SystemPanel();
        this.memon  = new MemMon();
        
		addTab( "Help"   , this.help          );
		addTab( "System" , this.system        );
		addTab( "MemMon" , this.memon         );
		addTab( "About"  , new AboutPanel()   );
		addTab( "License", new LicensePanel() );
        
        addChangeListener( new ChangeListener()
        {
            public void stateChanged( ChangeEvent evt )
            {
                int nTab = JHelp.this.getSelectedIndex();
                
                switch( nTab )
                {
                    case 1: JHelp.this.system.fillTable();    break;
                    case 2: JHelp.this.memon.updateButtons(); break;
                }
            }
        });
	}

	/**
	 * Changes the content of the Help pane.
	 * 
	 * @param sContext One of the constants defined in this class.
	 */
	public void setContext( String sContext )
	{
		this.help.setContext( sContext );
	}

	/**
	 * Loads an HTML file and return its content in a <code>JEditorPane</code>.
	 * <p>
	 * These are special files stored in <pre>&lt;help&gt;/extra</pre>
	 * and are normaly used to be shown when pressing a Help button.
	 * 
	 * @param sFileName File name to be shown
	 * @return The <code>JEditorPane</code> showing the file
	 */
	static JEditorPane getHtmlFile( String sFileName )
	{
		if( ! sFileName.endsWith( ".html" ) )
			sFileName += ".html";

		JEditorPane html = new JEditorPane();
		            html.setContentType( "text/html" );
		            html.setEditable( false );
		            html.setBackground( new Color( 0xFF, 0xFF, 0xDD ) );
		
        String sLanguage = new JWindow().getLocale().getLanguage();
        
		try
		{    
			html.setPage( "file:help/extra/"+ sLanguage +'/'+ sFileName );
		}
        catch( IOException exc )  // Localized help does not exists: then try with English
        {
            try
            {    
                html.setPage( "file:help/extra/en/"+ sFileName );
            }
            catch( IOException e )
            {
                html.setText( "<html><body>File: 'help/extra/"+ sLanguage +'/'+ sFileName +
                              "<br> Help file not found</body></html>" );
            }
		}

		return html;
	}
}